package com.jy.rhin.domain.document.filter.impl;/*package com.jy.rhin.domain.document.filter.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.JSONPath;
import com.jy.rhin.domain.document.filter.DocumentFilter;
import com.jy.rhin.domain.document.filter.DocumentFilterDescEnum;
import com.jy.rhin.domain.exception.RhinInfraErrorType;
import com.jy.rhin.infra.uitl.Asserts;
import com.jy.rhin.sdk.dto.document.DocumentContentDto;
import com.jy.rhin.sdk.dto.document.DocumentSubmissionSetDto;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.ReflectionUtils;

import javax.annotation.PostConstruct;
import java.lang.reflect.Field;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.*;

*//**
 * @author WeiQ
 * @date 2022/6/9 10:46
 * @Desc 入参字段格式校验：验证参数必填/值域
 *//*
@Slf4j
@Service
public class DocumentFilter4FieldFormat implements DocumentFilter {

    *//**
     * 提交集所有必填字段列表
     *//*
    List<Object> dtoNotEmptyFieldList = new ArrayList<>();

    *//**
     * 项目启动时加载提交集dto 获取所有必填字段的json路径
     *//*
    @PostConstruct
    private void init() {
        Class<?> clazz = DocumentSubmissionSetDto.class;
        fieldColl(clazz, dtoNotEmptyFieldList, "$");
    }

    *//**
     * 遍历、递归提交集以及提交集子类拿到所ApiModelProperty注解里标记必填的字段
     *
     * @param clazz
     * @param path
     *//*
    private void fieldColl(Class<?> clazz, List<Object> fieldList, String path) {
        if (!clazz.isAnnotationPresent(ApiModel.class)) {
            return;
        }
        Field[] fields = clazz.getDeclaredFields();
        for (Field field : fields) {
            if (!field.isAnnotationPresent(ApiModelProperty.class)) {
                continue;
            }
            //确保叶子顺序
            add(fieldList, path, field);
            Class<?> fieldClass = field.getType();
            if (List.class.isAssignableFrom(fieldClass)) {
                Type t = field.getGenericType();
                if (t instanceof ParameterizedType) {
                    ParameterizedType pt = (ParameterizedType) t;
                    Class clz = (Class) pt.getActualTypeArguments()[0];
                    List<Object> fieldList1 = new ArrayList<>();
                    fieldColl(clz, fieldList1, "$");
                    Map<String, List<Object>> map = new HashMap<>();
                    map.put(path + "." + field.getName(), fieldList1);
                    fieldList.add(map);
                }
            } else if (!BeanUtils.isSimpleProperty(fieldClass)) {
                fieldColl(fieldClass, fieldList, path + "." + field.getName());
            }
        }
    }

    *//**
     * 加载缓存所有必填字段
     * @param fieldList
     * @param path
     * @param field
     *//*
    private void add(List<Object> fieldList, String path, Field field) {
        ReflectionUtils.makeAccessible(field);
        ApiModelProperty annotation = field.getAnnotation(ApiModelProperty.class);
        //必填性校验 required()=true，则字段不能为空
        if (annotation.required()) {
            fieldList.add(path + "." + field.getName());
        }
    }

    @Override
    public boolean shouldFilter(DocumentSubmissionSetDto submissionSet, List<DocumentContentDto> contentList) {
        return true;
    }


    *//**
     * 通过反射，根据swagger注解，验证字段是否必填
     *
     * @param submissionSet
     * @param contentList
     *//*
    @Override
    public void doFilter(DocumentSubmissionSetDto submissionSet, List<DocumentContentDto> contentList) {
        checkList(dtoNotEmptyFieldList, JSON.toJSONString(submissionSet), "");
    }

    *//**
     * 根据提交集的json字符串以及启动加载的dtoNotEmptyFieldList验证必填字段都有值
     * @param list
     * @param jsonString
     * @param path
     *//*
    private void checkList(List<Object> list, String jsonString, String path){
        for (Object item : list) {
            if (item instanceof String) {
                Object fieldValue = JSONPath.read(jsonString, (String) item);
                Asserts.nonNull(fieldValue, RhinInfraErrorType.SUBMISSION_SET_FIELD_VALID_ERROR, String.format(": [%s]不能为空", item));
            } else if(item instanceof Map){
                Map<String, List<Object>> map = (Map<String, List<Object>>) item;
                Iterator<Map.Entry<String, List<Object>>> iterator = map.entrySet().iterator();
                while (iterator.hasNext()) {
                    Map.Entry<String, List<Object>> next = iterator.next();
                    String key = next.getKey();
                    //拼接字段全路径
                    path += key;
                    List<Object> value = next.getValue();
                    JSONArray jsonArray = JSONPath.read(jsonString, key, JSONArray.class);
                    //因所有节点都是$.xxxx 为了可读将最终全路径多个$替换
                    Asserts.nonNull(jsonArray, RhinInfraErrorType.SUBMISSION_SET_FIELD_VALID_ERROR, String.format(": [%s]不能为空", "$"+path.replace("$","")));
                    //遍历所有数据节点
                    for (Object obj : jsonArray) {
                        checkList(value, ((JSONObject) obj).toJSONString(), key);
                    }
                }
            }
        }
    }

    @Override
    public DocumentFilterDescEnum filterDesc() {
        return DocumentFilterDescEnum.FIELD_FORMAT;
    }
}
*/